package edu.kit.csl.pisa.datatypes;

import java.util.Arrays;

/*
This file is part of the PISA Alignment Tool.

Copyright (C) 2013
Karlsruhe Institute of Technology
Cognitive Systems Lab (CSL)
Felix Stahlberg

PISA is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PISA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PISA. If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * An instance of this class describes an alignment very space efficiently.
 * Alignment identifiers can be used in a hashmap to avoid duplicate neighbors
 * while keeping the space consumption at a minimum. Note that you cannot
 * calculate the alignment probability based only on the identifier. Note also
 * that the identifier abstracts from locks and the number of source language
 * words.
 */
public class AlignmentIdentifier {

	final short[] alignment;
	final byte[] lengths;
	
	/**
	 * Builds the identifier structure.
	 * 
	 * @param nTrgtWords number of target language words
	 * @param wordAlignment IBM style word alignment vector
	 * @param startPositions start positions of target words as stored in 
	 * 	{@link Alignment#startPositions}.
	 */
	public AlignmentIdentifier(int nTrgtWords, short[] wordAlignment, short[]
			startPositions) {
		alignment = new short[nTrgtWords];
		lengths = new byte[nTrgtWords];
		for (int i = 0; i < nTrgtWords; i++) {
			lengths[i] = (byte) (startPositions[i+1] - startPositions[i]);
			alignment[i] = wordAlignment[i];
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return 31 + Arrays.hashCode(alignment) + Arrays.hashCode(lengths);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		AlignmentIdentifier other = (AlignmentIdentifier) obj;
		return Arrays.equals(alignment, other.alignment)
				&& Arrays.equals(lengths, other.lengths);
	}
}
